package com.zq.assistant.ui.file.down;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

import com.zq.assistant.func.db.DBHelper;
import com.zq.assistant.func.db.table.DownloadInfo;
import com.zq.db.platform.android.dao.AndroidSQLiteDao;

public class FileDownloadProvider extends ContentProvider {

    private static final String AUTHORITIES = "com.zq.assistant.ui.file.down.FileDownloadProvider";

    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITIES + "/getDownloadInfo");

    private static UriMatcher uriMatcher;

    private static final int CODE_DOWNLOAD_INFO = 1;

    static {

        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITIES,"/getDownloadInfo",CODE_DOWNLOAD_INFO);
    }
    private AndroidSQLiteDao<DownloadInfo> dao;

    @Override
    public boolean onCreate() {

        dao = (AndroidSQLiteDao<DownloadInfo>) DBHelper.getDao(DownloadInfo.class);
        return true;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {

        int code = uriMatcher.match(uri);
        switch (code){
            case CODE_DOWNLOAD_INFO:

                int deleteCount = dao.delete(selection,selectionArgs);
                if(getContext() != null){
                    getContext().getContentResolver().notifyChange(Uri.withAppendedPath(uri,"/" + deleteCount),null);
                }
                return deleteCount;
            default:
                throw new UnsupportedOperationException(uri.toString());
        }
    }

    @Override
    public String getType(Uri uri) {
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {

        int code = uriMatcher.match(uri);
        switch (code){
            case CODE_DOWNLOAD_INFO:

                long rowId = dao.insert(null,values);
                uri = Uri.withAppendedPath(uri,"/" + rowId);
                if(getContext() != null){
                    getContext().getContentResolver().notifyChange(uri,null);
                }
                return uri;
            default:
                throw new UnsupportedOperationException(uri.toString());
        }
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

        int code = uriMatcher.match(uri);
        switch (code){
            case CODE_DOWNLOAD_INFO:

                Cursor cursor = dao.query(projection,selection,selectionArgs,null,null,sortOrder);
                if(cursor != null){
                    cursor.setNotificationUri(getContext().getContentResolver(),uri);
                }
                return cursor;
            default:
                throw new UnsupportedOperationException(uri.toString());
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

        int code = uriMatcher.match(uri);
        switch (code){
            case CODE_DOWNLOAD_INFO:

                int rowCount = dao.update(values,selection,selectionArgs);
                if(getContext() != null){
                    uri = Uri.withAppendedPath(uri,"/" + rowCount);
                    getContext().getContentResolver().notifyChange(uri,null);
                }
                return rowCount;
            default:
                throw new UnsupportedOperationException(uri.toString());
        }
    }
}
